package gemini.castor.ui.client.page.content.home.register.confirminfo;

import gemini.basic.model.User;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.home.register.RegisterEvent;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class ConfirmInfoFormPresenter extends
		CastorWidgetPresenter<ConfirmInfoFormPresenter.Display> {

	@Inject
	public ConfirmInfoFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getConfirmClickHandlers();
		HasClickHandlers getCancelClickHandlers();
		HasClickHandlers getBackClickHandlers();
		void setObject(ConfirmInfoFormObject inputObject);
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_REGISTER);
	private ConfirmInfoFormObject inputObject;

	@Override
	protected void onBind() {

		registerHandler(display.getConfirmClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						confirmRegisterUser();
					}
				}));
		
		registerHandler(display.getCancelClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new RegisterEvent(
								RegisterEvent.ForwardType.CANCEL));
					}
				}));
		
		registerHandler(display.getBackClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new RegisterEvent(
								RegisterEvent.ForwardType.BACK));
					}
				}));
		
		display.loadData();
	}
	
	@Override
	public void setInputObject(Object o){
		this.inputObject = (ConfirmInfoFormObject) o;
		display.setObject(inputObject);
	}

	protected void confirmRegisterUser() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getUserService().save(inputObject.getUser(),
				new AbstractAsyncCallback<User>() {

					@Override
					public void onSuccess(User result) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
						GuiUtils.info("Your submit is successfully!<br/>Please remember your ID and Pin to use Castor App!");
						localEventBus.fireEvent(new RegisterEvent(
								RegisterEvent.ForwardType.CONFIRM));
					}
				});		
		
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}
}
